package com.hktf.uilib.utils;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


import com.hktf.corelib.utils.CommUtils;
import com.hktf.corelib.utils.LogMds;
import com.hktf.corelib.utils.UIUtils;

import com.hktf.uilib.R;
import com.hktf.uilib.bean.ISelect;

import org.apache.commons.lang3.StringUtils;


/**
 * 弹框及关闭对话框工具类
 *
 * @author Administrator
 */
public class DlgUtils {
    private static Dialog dialog;



    public static void disMissDlg() {
        if (dialog != null) {
            try {
                dialog.dismiss();
            } catch (Exception e) {
                e.printStackTrace();
            }
            dialog = null;
        }

    }


    public static boolean isActivityFinish(Context context) {
        if (context == null) {
            return true;
        }
        if (context instanceof Activity) {
            Activity activity = (Activity) context;
            if (activity.isFinishing()) {
                return true;
            }
        }
        return false;
    }

















    /**
     * 警示弹窗
     */
    public static void showDlg(Context context, String messsage, String confirmStr, String cancelStr, final MyListener listener) {
        disMissDlg();
        try {
            dialog = new Dialog(context, R.style.MyAlertDialog);
            dialog.setCancelable(false);
            View v = View.inflate(context, R.layout.dialog_unregister_account, null);
            TextView tv_title = (TextView) v.findViewById(R.id.tv_title);
            TextView tv_msg = (TextView) v.findViewById(R.id.tv_msg);
            if (StringUtils.isNotBlank(messsage)) {
                tv_msg.setText(messsage);
            }
            tv_title.setText(context.getResources().getString(R.string.str_information));
            View line1 = v.findViewById(R.id.line1);
            View line2 = v.findViewById(R.id.line2);
            View line3 = v.findViewById(R.id.line3);
            tv_title.setTextColor(UIUtils.getColor(R.color.dialog_title));
            line1.setBackgroundColor(UIUtils.getColor(R.color.dialog_title));
            Button btn_yes = (Button) v.findViewById(R.id.btn_yes);
            if (StringUtils.isNotBlank(confirmStr)) {
                btn_yes.setText(confirmStr);
                btn_yes.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        disMissDlg();
                        listener.onConfirm();
                    }
                });
            } else {
                btn_yes.setVisibility(View.GONE);
                line3.setVisibility(View.GONE);
            }
            Button btn_no = (Button) v.findViewById(R.id.btn_no);
            if (StringUtils.isNotBlank(cancelStr)) {
                btn_no.setText(cancelStr);
                btn_no.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        disMissDlg();
                        listener.onCancel();
                    }
                });
            } else {
                btn_no.setVisibility(View.GONE);
                line3.setVisibility(View.GONE);
            }
            dialog.setContentView(v);
            Window window = dialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            //            lp.width = (int) DisplayUtil.dip2px(context, 500); // 设置宽度
            //lp.width =UIUtils.dip2px(250); // 设置宽度
            LogMds.e("DialogUtil", "width=" + lp.width);
            window.setAttributes(lp);
            dialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 警示弹窗
     */
    public static void showDlgNew(Context context, String title, String messsage, String confirmStr, String cancelStr, final MyListener listener) {
        showDlgNew(context, title, messsage, confirmStr, cancelStr, false, listener);
    }
    /**
     * 提示信息弹窗
     */
    public static void showDlgNew(Context context, String title, String messsage, String confirmStr, String cancelStr,boolean isCenter, final MyListener listener) {
        disMissDlg();
        try {
            dialog = new Dialog(context, R.style.MyAlertDialog);
            dialog.setCancelable(false);
            View v = View.inflate(context, R.layout.dialog_unregister_account, null);
            TextView tv_title = (TextView) v.findViewById(R.id.tv_title);
            TextView tv_msg = (TextView) v.findViewById(R.id.tv_msg);
            tv_msg.setGravity(isCenter?Gravity.CENTER:Gravity.LEFT);
            View line = v.findViewById(R.id.line);
            if (StringUtils.isNotBlank(messsage)) {
                tv_msg.setText(messsage);
            }
            if (StringUtils.isNotBlank(title)) {
                tv_title.setText(title);
            }
            Button btn_yes = (Button) v.findViewById(R.id.btn_yes);
            if (StringUtils.isNotBlank(confirmStr)) {
                btn_yes.setText(confirmStr);
                btn_yes.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        disMissDlg();
                        if (listener != null) {

                            listener.onConfirm();
                        }
                    }
                });
            } else {
                btn_yes.setVisibility(View.GONE);
                line.setVisibility(View.GONE);
            }
            Button btn_no = (Button) v.findViewById(R.id.btn_no);
            if (StringUtils.isNotBlank(cancelStr)) {
                btn_no.setText(cancelStr);
                btn_no.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        disMissDlg();
                        if (listener != null) {

                            listener.onCancel();
                        }
                    }
                });
            } else {
                btn_no.setVisibility(View.GONE);
                line.setVisibility(View.GONE);
            }
            dialog.setContentView(v);
            Window window = dialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = (int) DisplayUtil.getMetricsWidth(context) - DisplayUtil.dip2px(context, 72); // 设置宽度
            //lp.width =UIUtils.dip2px(250); // 设置宽度
            LogMds.e("DialogUtil", "width=" + lp.width);
            window.setAttributes(lp);
            dialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    /**
     * 重命名
     *
     * @param context
     */
    public static void showEditDialog(final Context context, String defaultValue, String title, String msg,Integer limit, final RenameFileCallback listener) {

        disMissDlg();
        try {
            dialog = new Dialog(context, R.style.MyAlertDialog);
            dialog.setCancelable(false);
            View v = View.inflate(context, R.layout.edit_dialog, null);
            TextView tv_title = v.findViewById(R.id.tv_title);
            if (title == null) {
                tv_title.setVisibility(View.GONE);
            } else if (StringUtils.isNotEmpty(title)) {
                tv_title.setText(title);
            }
            TextView tv_tip = v.findViewById(R.id.tv_tip);
            if (msg == null) {
                tv_tip.setVisibility(View.GONE);
            } else {
                tv_tip.setText(msg);
            }

            final EditText et = (EditText) v.findViewById(R.id.et);
            UIUtils.setEditTextMaxLength(et,limit);
            et.setText(defaultValue == null ? "" : defaultValue);
            Button btn_cancel = (Button) v.findViewById(R.id.btn_cancel);
            btn_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CommUtils.closeInput(et);
                    disMissDlg();
                }
            });
            Button bt_confirm = (Button) v.findViewById(R.id.btn_confirm);
            bt_confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CommUtils.closeInput(et);
                    disMissDlg();
                    String name = et.getText().toString().trim();
                    listener.onConfirm(name);
                }
            });
            dialog.setContentView(v);
            Window window = dialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
//            lp.width = (int) DisplayUtil.getMetricsWidth(context) - DisplayUtil.dip2px(context, 30); // 设置宽度
            lp.width = UIUtils.dip2px(300); // 设置宽度
            LogMds.e("DialogUtil", "width=" + lp.width);
            window.setAttributes(lp);
            dialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static interface SelectListener {
        void onSelect(ISelect selectItem, int position);
    }


    private static Handler handler;



    /**
     * 等待提示框
     *
     * @param msg
     */
    public static void showLoadingDlg(String msg, Context context) {
        disMissDlg();
        try {
            dialog = new Dialog(context, R.style.MyAlertDialog);
            dialog.setCancelable(false);
            View v = View.inflate(context, R.layout.loading_dialog, null);
            final TextView tv1 = v.findViewById(R.id.tv1);
            if (!TextUtils.isEmpty(msg)) {
                tv1.setText(msg);
            }
            Button btn_cancel = (Button) v.findViewById(R.id.btn_cancel);
            btn_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    disMissDlg();
                }
            });
            Button bt_confirm = (Button) v.findViewById(R.id.btn_confirm);
            bt_confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            });
            dialog.setContentView(v);
            Window window = dialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = (int) DisplayUtil.getMetricsWidth(context) - DisplayUtil.dip2px(context, 30); // 设置宽度
            LogMds.e("DialogUtil", "width=" + lp.width);
            window.setAttributes(lp);
            dialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 选择框
     */
    public static void showPickDlg(Context context, String[] items, Boolean cancelable, final PickListener listener) {
        if (items == null || items.length < 2) {
            return;
        }
        disMissDlg();
        try {
            dialog = new Dialog(context, R.style.MyAlertDialog);
            dialog.setCancelable(cancelable);
            View conentView = View.inflate(context, R.layout.pickpop, null);

            Button pick_one = (Button) conentView.findViewById(R.id.pick_one);
            Button pick_two = (Button) conentView.findViewById(R.id.pick_two);

            if (!TextUtils.isEmpty(items[0])) {
                pick_one.setText(items[0]);
            }
            if (!TextUtils.isEmpty(items[1])) {
                pick_two.setText(items[1]);
            }
            if (items.length == 3 && !TextUtils.isEmpty(items[2])) {
                Button pick_three = (Button) conentView.findViewById(R.id.pick_three);
                TextView tvline = conentView.findViewById(R.id.tv_line);
                pick_three.setVisibility(View.VISIBLE);
                tvline.setVisibility(View.VISIBLE);
                pick_three.setText(items[2]);
                pick_three.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        disMissDlg();
                        if (listener != null) {
                            listener.onPick(2);
                        }
                    }
                });
            }
            Button pick_cancel = (Button) conentView.findViewById(R.id.pick_cancel);
            pick_one.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    disMissDlg();
                    if (listener != null) {
                        listener.onPick(0);
                    }
                }
            });
            pick_cancel.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    disMissDlg();
                    if (listener != null) {
                        listener.onCancel();
                    }
                }
            });
            pick_two.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    disMissDlg();
                    if (listener != null) {
                        listener.onPick(1);
                    }
                }
            });
            if (cancelable) {
                conentView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        disMissDlg();
                        if (listener != null) {
                            listener.onCancel();
                        }
                    }
                });
            }
            dialog.setContentView(conentView);
            Window window = dialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = (int) DisplayUtil.getMetricsWidth(context); // 设置宽度
            LogMds.e("DialogUtil", "width=" + lp.width);
            window.setAttributes(lp);
            dialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 带文字的等待提示
     */
    public static void showWaitDlg(Context context, String msg) {
        disMissDlg();
        try {
            dialog = new Dialog(context, R.style.MyAlertDialog);
            dialog.setCancelable(false);
            View conentView = View.inflate(context, R.layout.progress_bar_dlg, null);

            TextView tv_msg = conentView.findViewById(R.id.tv_msg);
            if (msg != null) {
                tv_msg.setText(msg);
            }

            dialog.setContentView(conentView);
            Window window = dialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            //            lp.width = (int) DisplayUtil.getMetricsWidth(context); // 设置宽度
            lp.width = UIUtils.dip2px(300); // 设置宽度
            LogMds.e("DialogUtil", "width=" + lp.width);
            window.setAttributes(lp);
            dialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 重命名
     *
     * @param context
     */
    public static void showWxTip(final Context context, final MyListener listener) {
        disMissDlg();
        try {
            dialog = new Dialog(context, R.style.MyAlertDialog);
            dialog.setCancelable(false);
            View v = View.inflate(context, R.layout.wx_tip, null);
            Button bt_confirm = (Button) v.findViewById(R.id.btn_confirm);
            bt_confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onConfirm();
                    }
                    disMissDlg();
                }
            });
            dialog.setContentView(v);
            Window window = dialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = (int) DisplayUtil.getMetricsWidth(context); // 设置宽度
            LogMds.e("DialogUtil", "width=" + lp.width);
            window.setAttributes(lp);
            dialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }





    /**
     * 蓝牙连接等待提示
     */
    public static void showPic(Context context, int resId, String msg, final MyListener listener) {
        disMissDlg();
        try {
            dialog = new Dialog(context, R.style.dialogStyle_light);
            dialog.setCancelable(false);
            View conentView = View.inflate(context, R.layout.c90_nopay, null);

            TextView tv_msg = conentView.findViewById(R.id.tv_msg);
            if (StringUtils.isNotBlank(msg)) {
                tv_msg.setText(msg);
            } else {
                tv_msg.setText("");
            }
            TextView tv_confirm = conentView.findViewById(R.id.tv_confirm);
            tv_confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DlgUtils.disMissDlg();
                    if (listener != null) {
                        listener.onConfirm();
                    }
                }
            });

            ImageView iv = conentView.findViewById(R.id.iv);
            iv.setImageResource(resId);

            dialog.setContentView(conentView);
            //            Window window = dialog.getWindow();
            //            WindowManager.LayoutParams lp = window.getAttributes();
            //            lp.width = (int) DisplayUtil.getMetricsWidth(context); // 设置宽度
            //            lp.height = (int) DisplayUtil.getMetricsHeight(context); // 设置宽度
            ////            lp.width = UIUtils.dip2px(600); // 设置宽度
            //            LogMds.e("DialogUtil", "width=" + lp.width);
            //            window.setAttributes(lp);
            dialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 蓝牙连接等待提示
     */
    public static void showPic(Context context, int resId, final MyListener listener) {
        disMissDlg();
        try {
            dialog = new Dialog(context, R.style.dialogStyle_light);
            dialog.setCancelable(false);
            View conentView = View.inflate(context, R.layout.pic_dialog_layout, null);

            TextView tv_confirm = conentView.findViewById(R.id.tv_confirm);
            tv_confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DlgUtils.disMissDlg();
                    if (listener != null) {
                        listener.onConfirm();
                    }
                }
            });

            ImageView iv = conentView.findViewById(R.id.iv);
            iv.setImageResource(resId);

            dialog.setContentView(conentView);
            Window window = dialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
//            lp.width = (int) DisplayUtil.getMetricsWidth(context); // 设置宽度
//            lp.height = (int) DisplayUtil.getMetricsHeight(context); // 设置宽度
//            lp.width = UIUtils.dip2px(600); // 设置宽度
            LogMds.e("DialogUtil", "width=" + lp.width);
            window.setAttributes(lp);
            dialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 蓝牙连接等待提示
     */
    private static ProgressDialog createDialog(Context context) {
        disMissDlg();
        try {
            dialog = new ProgressDialog(context, R.style.dialogStyle_light);
            dialog.setCancelable(false);

            Window window = dialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
//            lp.width = (int) DisplayUtil.getMetricsWidth(context); // 设置宽度
//            lp.height = (int) DisplayUtil.getMetricsHeight(context); // 设置宽度
//            lp.width = UIUtils.dip2px(600); // 设置宽度
            LogMds.e("DialogUtil", "width=" + lp.width);
            window.setAttributes(lp);
            dialog.show();
            return (ProgressDialog) dialog;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void showLoading(Context context,String msg){
        DlgUtils.showLoading(context,msg+"",false,null);
    }
    public static void showLoading1(Context context){
        DlgUtils.showLoading(context,context.getString(R.string.wait_common),false,null);
    }
    public static void showLoading2(Context context){
        DlgUtils.showLoading(context,context.getString(R.string.wait_common_get),false,null);
    }

    /**
     * 加载中提示框
     *
     * @param context
     */
    public static void showLoading(final Context context, String msg, boolean showCancel,final MyListener listener) {
        disMissDlg();
        try {
            dialog = new Dialog(context, R.style.MyAlertDialog);
            dialog.setCancelable(false);
            View v = View.inflate(context, R.layout.dialog_loading, null);
            TextView tv =v.findViewById(R.id.tv_msg);
            tv.setText(msg);

            Button btn = (Button) v.findViewById(R.id.btn);
            if (showCancel) {
                btn.setVisibility(View.VISIBLE);
            }
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    disMissDlg();
                    if (listener != null) {
                        listener.onCancel();
                    }
                }
            });

            dialog.setContentView(v);
            Window window = dialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = (int) DisplayUtil.getMetricsWidth(context) - DisplayUtil.dip2px(context, 100); // 设置宽度
            LogMds.e("DialogUtil", "width=" + lp.width);
            window.setAttributes(lp);
            dialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**


    /**
     * 警示弹窗
     */
    public static void showDlgInfo(Context context, String title, String messsage, String confirmStr,final MyListener listener) {
        disMissDlg();
        try {
            dialog = new Dialog(context, R.style.MyAlertDialog);
            dialog.setCancelable(false);
            View v = View.inflate(context, R.layout.dialog_prototype, null);
            TextView tv_title = (TextView) v.findViewById(R.id.tv_title);
            TextView tv_msg = (TextView) v.findViewById(R.id.tv_msg);
            if (StringUtils.isNotBlank(messsage)) {
                tv_msg.setText(messsage);
            }
            tv_title.setText(context.getResources().getString(R.string.str_information));
            if (StringUtils.isNotBlank(title)) {
                tv_title.setVisibility(View.VISIBLE);
                tv_title.setText(title);
            }else {
                tv_title.setVisibility(View.GONE);
            }
            Button btn_yes = (Button) v.findViewById(R.id.btn_yes);
            if (StringUtils.isNotBlank(confirmStr)) {
                btn_yes.setText(confirmStr);
                btn_yes.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        disMissDlg();
                        if (listener != null) {
                            listener.onConfirm();
                        }
                    }
                });
            } else {
                btn_yes.setVisibility(View.GONE);
            }

            v.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    disMissDlg();
                    if (listener != null) {
                        listener.onCancel();
                    }
                }
            });

            dialog.setContentView(v);
            Window window = dialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            //            lp.width = (int) DisplayUtil.dip2px(context, 500); // 设置宽度
            //            lp.width =UIUtils.dip2px(300); // 设置宽度
            LogMds.e("DialogUtil", "width=" + lp.width);
            window.setAttributes(lp);
            dialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 警示弹窗
     */
    public static void showDlgInfoNew(Context context, String title, String messsage, String confirmStr,String canCel,final MyListener listener) {
        disMissDlg();
        try {
            dialog = new Dialog(context, R.style.MyAlertDialog);
            dialog.setCancelable(false);
            View v = View.inflate(context, R.layout.dialog_info_new, null);
            TextView tv_title = (TextView) v.findViewById(R.id.tv_title);
            TextView tv_msg = (TextView) v.findViewById(R.id.tv_msg);
            if (StringUtils.isNotBlank(messsage)) {
                tv_msg.setText(messsage);
            }
            tv_title.setText(context.getResources().getString(R.string.str_information));
            if (StringUtils.isNotBlank(title)) {
                tv_title.setVisibility(View.VISIBLE);
                tv_title.setText(title);
            }else {
                tv_title.setVisibility(View.GONE);
            }
            Button btn_yes = (Button) v.findViewById(R.id.btn_yes);
            if (StringUtils.isNotBlank(confirmStr)) {
                btn_yes.setText(confirmStr);
                btn_yes.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        disMissDlg();
                        if (listener != null) {
                            listener.onConfirm();
                        }
                    }
                });
            } else {
                btn_yes.setVisibility(View.GONE);
            }

            Button btn_cancel = (Button) v.findViewById(R.id.btn_cancel);
            if (StringUtils.isNotBlank(canCel)) {
                btn_cancel.setText(canCel);
                btn_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        disMissDlg();
                        if (listener != null) {
                            listener.onCancel();
                        }
                    }
                });
            } else {
                btn_cancel.setVisibility(View.GONE);
            }

            dialog.setContentView(v);
            Window window = dialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            //            lp.width = (int) DisplayUtil.dip2px(context, 500); // 设置宽度
            //            lp.width =UIUtils.dip2px(300); // 设置宽度
            LogMds.e("DialogUtil", "width=" + lp.width);
            window.setAttributes(lp);
            dialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 警示弹窗
     */
    public static void showProtocol(Context context, final PickListener listener) {
        disMissDlg();
        try {
            dialog = new Dialog(context, R.style.MyAlertDialog);
            dialog.setCancelable(false);
            dialog.setCanceledOnTouchOutside(false);
            View v = View.inflate(context, R.layout.dialog_protocol, null);

            v.findViewById(R.id.tv1).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onPick(1);
                    }
                }
            });
            v.findViewById(R.id.tv2).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onPick(2);
                    }
                }
            });

            TextView tv3 = v.findViewById(R.id.tv3);
            tv3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    disMissDlg();
                    if (listener != null) {
                        listener.onPick(3);
                    }
                }
            });
            TextView tv4 = v.findViewById(R.id.tv4);
            tv4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    disMissDlg();
                    if (listener != null) {
                        listener.onPick(4);
                    }
                }
            });
            dialog.setContentView(v);
            Window window = dialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            //            lp.width = (int) DisplayUtil.dip2px(context, 500); // 设置宽度
            //            lp.width =UIUtils.dip2px(300); // 设置宽度
            lp.width =DisplayUtil.getMetricsWidth(context)-UIUtils.dip2px(64); // 设置宽度
            LogMds.e("DialogUtil", "width=" + lp.width);
            window.setAttributes(lp);
            dialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 警示弹窗
     */
    public static void showProtocol2(Context context, final PickListener listener) {
        disMissDlg();
        try {
            dialog = new Dialog(context, R.style.MyAlertDialog);
            dialog.setCancelable(false);
            dialog.setCanceledOnTouchOutside(false);
            View v = View.inflate(context, R.layout.dialog_protocol2, null);

            v.findViewById(R.id.tv1).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onPick(1);
                    }
                }
            });
            v.findViewById(R.id.tv2).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onPick(2);
                    }
                }
            });

            TextView tv3 = v.findViewById(R.id.tv3);
            tv3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    disMissDlg();
                    if (listener != null) {
                        listener.onPick(3);
                    }
                }
            });
            TextView tv4 = v.findViewById(R.id.tv4);
            tv4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    disMissDlg();
                    if (listener != null) {
                        listener.onPick(4);
                    }
                }
            });
            dialog.setContentView(v);
            Window window = dialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            //            lp.width = (int) DisplayUtil.dip2px(context, 500); // 设置宽度
            lp.width =DisplayUtil.getMetricsWidth(context)-UIUtils.dip2px(64); // 设置宽度
            LogMds.e("DialogUtil", "width=" + lp.width);
            window.setAttributes(lp);
            dialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 请求权限弹窗
     */
    public static void requestPermission(Context context, String title, String messsage, final MyListener listener) {
        disMissDlg();
        try {
            dialog = new Dialog(context, R.style.MyAlertDialog);
            dialog.setCancelable(false);
            View v = View.inflate(context, R.layout.dialog_permission, null);
            TextView tv_title = (TextView) v.findViewById(R.id.tv_title);
            TextView tv_msg = (TextView) v.findViewById(R.id.tv_msg);
            if (StringUtils.isNotBlank(messsage)) {
                tv_msg.setText(messsage);
            }
            tv_title.setText(context.getResources().getString(R.string.str_information));
            if (StringUtils.isNotBlank(title)) {
                tv_title.setText(title);
            }
            Button btn_yes = (Button) v.findViewById(R.id.btn_yes);
            btn_yes.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    disMissDlg();
                    if (listener != null) {
                        listener.onConfirm();
                    }
                }
            });
            Button btn_no = (Button) v.findViewById(R.id.btn_no);
            btn_no.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    disMissDlg();
                    if (listener != null) {
                        listener.onCancel();
                    }
                }
            });
            dialog.setContentView(v);
            Window window = dialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            //            lp.width = (int) DisplayUtil.dip2px(context, 500); // 设置宽度
            //            lp.width =UIUtils.dip2px(300); // 设置宽度
            LogMds.e("DialogUtil", "width=" + lp.width);
            window.setAttributes(lp);
            dialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void showLoadingSend(Context context) {
        DlgUtils.showLoading(context, context.getString(R.string.wait_common), false, null);
    }

    public static void showLoadingGet(Context context) {
        DlgUtils.showLoading(context, context.getString(R.string.wait_common_get), false, null);
    }



    public interface PickListener {
        void onPick(int index);

        void onCancel();
    }

    public interface MyListener {
        void onConfirm();

        void onCancel();
    }

    public interface RenameFileCallback {
        void onConfirm(String name);

        void onCancel();
    }

    public static void dissmissDialog(Dialog dialog) {
        if (dialog != null) {
            try {
                dialog.dismiss();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
